package com.woniu.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.user.commons.Result;
import com.woniu.user.model.Account;
import com.woniu.user.model.Coach;
import com.woniu.user.model.Sign;
import com.woniu.user.service.CoachService;
import com.woniu.user.service.SignService;
import com.woniu.user.service.dto.AccountDto;
import com.woniu.user.service.dto.SignCoachDto;
import com.woniu.user.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 签约表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/sign")
@Slf4j
public class SignController {
    @Resource
    private SignService signService;

    @GetMapping("/findMyStu")
    public Result findMyStu(String condition, Integer current, Integer size, HttpServletRequest req) {
        Integer userId = JwtUtil.getUserIdFromReq(req);// 获取用户账号
        log.info("修改教练个人信息{}", userId);
        IPage<Account> myStu = signService.findMyStu(userId, condition, current, size);
        return Result.success(200, "查询成功", myStu);
    }

    @DeleteMapping("/delStu/{id}")
    public Result delStu(@PathVariable("id") Integer accountId, HttpServletRequest req) {
        Integer userId = JwtUtil.getUserIdFromReq(req);// 获取用户账号
        log.info("教练{}删除了学员{}", userId, accountId);
        signService.delStu(userId, accountId);
        return Result.success(200, "删除成功", null);
    }

    // 签约
    @PostMapping("build")
    public Result build(Integer coachId, HttpServletRequest req) {
        log.info("用户发起签约{} {}", coachId, req);
        Integer userId = JwtUtil.getUserIdFromReq(req);
        signService.build(coachId, userId);
        return Result.success(200, "签约成功", null);
    }

    // 解约
    @PostMapping("broke")
    public Result broke(Integer coachId, HttpServletRequest req) {
        log.info("用户发起解约");
        Integer userId = JwtUtil.getUserIdFromReq(req);
        signService.dissmiss(userId, coachId);
        return Result.success(200, "解约成功", null);
    }

    @GetMapping("/findMyCoach")
    public Result findMyCoach(String condition, Integer current, Integer size, HttpServletRequest req) {
        log.info("查找我的教练{}, {}, {}", current, size, req);
        Integer userId = JwtUtil.getUserIdFromReq(req);// 获取用户账号
        IPage<SignCoachDto> myCoach = signService.findMyCoach(userId, condition, current, size);
        return Result.success(200, "查询成功", myCoach);
    }

    // 查找我的所有签约教练
    @GetMapping("/getMyCoach")
    public Result getMyCoach(Integer page, Integer size, HttpServletRequest req) {
        log.info("查找我的教练{}, {}, {}", page, size, req);
        Integer userId = JwtUtil.getUserIdFromReq(req);// 获取用户账号
        Page<Sign> pageInfo = new Page<>(page, size);
        IPage<SignCoachDto> myCoach = signService.getMyCoach(pageInfo, userId);
        return Result.success(200, "查询成功", myCoach);
    }
}

